// 常用的方法
/**
 * 复制到剪贴板
 * @method copyText 复制到剪贴板
 * @param {string} text 复制的内容
 */
export const copyText = (text: string) => {
    if (navigator.clipboard) {
        return navigator.clipboard.writeText(text).then(() => {
            console.log('复制成功');
        }).catch((error) => {
            throw new TypeError(
                `复制失败,请检查是否开启了剪贴板权限`
            );
        });
    }
}

/**
 * @method debounce 防抖函数
 * @param {Function} callback 回调函数
 * @param {Number} wait 毫秒
 * @returns {Function} callback 回调函数
 */
export const debounce = ((callback, wait = 800) => {
    let timer: ReturnType<typeof setTimeout> | null = null
    return (callback: Function, wait: number) => {
        timer && clearTimeout(timer)
        timer = setTimeout(callback, wait)
    }
})()
/**
* @method throttle  节流函数
 * @param {Function} callback 回调函数
 * @param {Number} wait 秒数
 * @returns {Function} callback 回调函数
 */
export const throttle = ((callback, wait) => {
    let last = 0
    return (callback: Function, wait = 800) => {
        let now = +new Date()
        if (now - last > wait) {
            callback()
            last = now
        }
    }
})()

/**
 * 手机号脱敏
 * @param {Number|String} mobile 手机号
 * @returns {Number|String} 13312341234 => 133****1234
 */
export const hideMobile = (mobile: number | string) => {
    return mobile.toString().replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2")
}

/**
 * 大小写转换
 * @param {String} str 需要转换的字符串
 * @param {Number} type 转换类型
 * @returns {String} 返回转换好的字符串
 * type 1-全小写 2-全大写 3-首字母大写
 */
export const turnCase = (str: string, type = 1) => {
	switch (type) {
		case 1:
			return str.toLowerCase()
		case 2:
			return str.toUpperCase()
		case 3:
            return str ? str[0]?.toUpperCase() + str.substring(1).toLowerCase() : '';
		default:
			return str
	}
}